import React from 'react';
import { Form, Row, Col, message } from 'antd';
import { fieldList, apiPath } from '../private.config.data';
import QuickTool from '@/utils/Engineering/QuickTool';
import ApiTubro from '@/utils/Engineering/ApiTubro';
const FormItem = Form.Item;

class SingleMainPart extends React.Component {
  componentDidMount() {
    this.props.wrappedComponentRef && this.props.wrappedComponentRef(this);
  }

  async onSubmit(id: any) {
    let isok;
    const { validateFields } = this.props.form;
    await validateFields(async (err: any, values: any) => {
      if (err) return message.error('填写的参数不完整');
      let { editable, originalRecord, ...__sent_data } = values;
      isok = await ApiTubro.onSingleUpdate(apiPath.submitDataBaseURL, {
        __sent_data: { ...__sent_data, id },
      });
    });
    return isok;
  }

  render() {
    const { getFieldDecorator } = this.props.form;

    return (
      <>
        <Form>
          <Row>
            {fieldList
              // .sort((a, b) => a.sort - b.sort)
              .map(item => (
                <Col span={item.col || 12}>
                  <FormItem
                    key={item.dataIndex_or_field}
                    label={item.title_or_label}
                    labelAlign={item.labelAlign || 'right'}
                    layout={item.layout || 'inline'}
                    style={item.style || { width: '100%' }}
                    {...QuickTool.getformItemLayout(item)}
                  >
                    {getFieldDecorator(item.dataIndex_or_field, {
                      initialValue: item.initialValue || undefined,
                      rules: [
                        { required: item.required ? true : false, message: item.message || null },
                      ],
                    })(QuickTool.getFormFieldNodeRender(item))}
                  </FormItem>
                </Col>
              ))}
          </Row>
        </Form>
      </>
    );
  }
}
export default Form.create({
  name: 'global_state',
  onFieldsChange(props, changedFields) {},
  mapPropsToFields(props) {
    let obj = {};
    if (props.allFieldToValueObj) {
      for (let [k, v] of Object.entries(props.allFieldToValueObj)) {
        obj[k] = Form.createFormField({
          value: v,
        });
      }
      return obj;
    } else {
      return {};
    }
  },
  onValuesChange(_, values) {},
})(SingleMainPart);
